#include "ipc.h"
#include <unistd.h>  // 确保包含unistd.h
#include <stdlib.h>
#include <time.h>

void supplier() {
    material_t materials[3] = {TOBACCO, PAPER, MATCH};
    int i, j;

    srand(time(NULL));  // 初始化随机数生成器

    while (1) {
        sem_down(sem_id);
       // printf("Supplier: Got semaphore\n");
        if (!shm_data->ready) {
            i = rand() % 3;
            j = (i + 1 + rand() % 2) % 3;
            shm_data->materials[0] = materials[i];
            shm_data->materials[1] = materials[j];
            shm_data->ready = 1;
            printf("Supplier provides: %d and %d\n", materials[i], materials[j]);
        }
        sem_up(sem_id);
       // printf("Supplier: Released semaphore\n");
        sleep(1);
    }
}

int main() {
    init_ipc();
    supplier();
    cleanup_ipc();
    return 0;
}
